package com.hulk.ratel.web.initializer;


import cn.hutool.core.date.DatePattern;
import com.hulk.ratel.common.util.SpringContextHolder;
import com.hulk.ratel.web.interceptor.SecurityInterceptor;
import net.dreamlu.mica.core.convert.EnumToStringConverter;
import net.dreamlu.mica.core.convert.StringToEnumConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 作者 hulk
 * @version 创建时间：2016年7月6日 下午4:50:19 类说明
 */



@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class WebMvcConfig implements WebMvcConfigurer {


	/**
	 * 增加GET请求参数中时间类型转换
	 * <ul>
	 * <li>HH:mm:ss -> LocalTime</li>
	 * <li>yyyy-MM-dd -> LocalDate</li>
	 * <li>yyyy-MM-dd HH:mm:ss -> LocalDateTime</li>
	 * </ul>
	 *
	 * @param registry
	 */
	@Override
	public void addFormatters(FormatterRegistry registry) {
		// 注册枚举转换，规则同 jackson
		registry.addConverter(new EnumToStringConverter());
		registry.addConverter(new StringToEnumConverter());
		// 注册时间的转换
		DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
		registrar.setTimeFormatter(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN));
		registrar.setDateFormatter(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
		registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
		registrar.registerFormatters(registry);
	}

	/**
	 *  解决中文乱码问题
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		WebMvcConfigurer.super.configureMessageConverters(converters);
		StringHttpMessageConverter converter = new StringHttpMessageConverter(
				StandardCharsets.UTF_8);
		converters.add(converter);
	}


	/**
	 * 避免form 提交 context-type 不规范中文乱码
	 * @return Filter
	 */
	@Bean
	public OrderedCharacterEncodingFilter characterEncodingFilter() {
		OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(StandardCharsets.UTF_8.name());
		filter.setForceEncoding(true);
		filter.setOrder(Ordered.HIGHEST_PRECEDENCE);
		return filter;
	}
	@Bean
	public SpringContextHolder SpringContextHolder() {
		return new SpringContextHolder();
	}

	@Bean
	public SecurityInterceptor securityInterceptor(){
		return new SecurityInterceptor();
	}


	@Override
	public void addInterceptors(InterceptorRegistry registry ) {

		registry.addInterceptor(securityInterceptor()).addPathPatterns("/**").excludePathPatterns("/rest/heartbeat").excludePathPatterns("/favicon.ico");

	}



}
